<template>
  <div class="roles">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <el-table :data="rolesList" style="width: 100%" stripe border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item1, index) in scope.row.children" :key="item1.id" class="border_bottom" :class="index == 0? 'border_top' : ''" type="flex" align="middle">
              <!-- 展示一级权限 -->
              <el-col :span="5">
                <el-tag>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 展示二级权限 -->
              <el-col :span="19">
                <el-row v-for="(item2, index) in item1.children" :key="item2.id" type="flex" align="middle" :class="index == 0? '' : 'border_top'">
                  <el-col :span="6">
                    <el-tag type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 展示三级 -->
                  <el-col :span="18">
                    <el-tag type="warning" v-for="item3 in item2.children" :key="item3.id" closable @close="clearTag(scope.row, item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRights(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="提示" :visible.sync="setRightsDialogVisible" width="50%" @close="resetDefsKeys">
        <el-tree ref="tree" :data="rolesTree" :props="defaultProps" show-checkbox default-expand-all :default-checked-keys="defKeys" node-key="id"></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRightsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subSetRights">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rolesList: [],
      setRightsDialogVisible: false,
      rolesTree: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [],
      roleId: ''
    }
  },

  created () {
    this.getRolesList()
  },

  methods: {
    async getRolesList () {
      const { data: res } = await this.$axios.get('roles')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rolesList = res.data
    },
    clearTag (row, id2) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete(`roles/${row.id}/rights/${id2}`)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        row.children = res.data
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    showSetRights (row) {
      this.setRightsDialogVisible = true
      this.roleId = row.id
      this.getAllKeys(row)
      this.getRolesTree()
    },
    getAllKeys (row) {
      if (!row.children) return this.defKeys.push(row.id)
      row.children.forEach(item => this.getAllKeys(item))
    },
    async getRolesTree () {
      const { data: res } = await this.$axios.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rolesTree = res.data
    },
    resetDefsKeys () {
      this.defKeys = []
    },
    async subSetRights () {
      // 1: 角色id
      // 2：所有授权的权限id （获取到三级的权限id、所有的父级分类id）
      // 获取所有的全选节点
      const keys = this.$refs.tree.getCheckedKeys()
      // 获取所有的半选节点
      const halfKeys = this.$refs.tree.getHalfCheckedKeys()
      const defKeys = [...keys, ...halfKeys]
      const { data: res } = await this.$axios.post(`roles/${this.roleId}/rights`, {
        rids: defKeys.join(',')
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.setRightsDialogVisible = false
      this.getRolesList()
    }
  }
}
</script>

<style scoped lang='less'>
  .el-tag {
    margin: 7px;
  }

  .border_top {
    border-top: 1px solid #eee;
  }

  .border_bottom{
    border-bottom: 1px solid #eee;
  }
</style>
